package com.jie.myvhr.service;


import com.jie.myvhr.bean.Hr;
import com.jie.myvhr.common.HrUtils;
import com.jie.myvhr.mapper.HrMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author jie
 * @date 2019/6/28 -10:16
 */
@Service
public class HrService implements UserDetailsService {

    @Resource
    private HrMapper hrMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Hr hr = hrMapper.loadUserByUsername(s);
        if (null == hr){
            throw new UsernameNotFoundException("用户不存在");
        }
        return hr;
    }

    public List<Hr> getAllHrExceptAdmin(){
        return hrMapper.getAllHr(HrUtils.getCurrentHr().getId());
    }

    public List<Hr> getAllHr() {
        return hrMapper.getAllHr(null);
    }

    public List<Hr> getHrsByKeywords(String keywords) {
        return hrMapper.getHrsByKeywords(keywords);
    }
    public int updateHr(Hr hr) {
        return hrMapper.updateHr(hr);
    }

    public int updateHrRoles(Long hrId, Long[] rids) {
        int i = hrMapper.deleteRoleByHrId(hrId);
        return hrMapper.addRolesForHr(hrId, rids);
    }

    public int deleteHr(Long hrId) {
        return hrMapper.deleteHr(hrId);
    }
}
